import { Injectable, } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Param } from './param.model';
import { RESPONSE_CODE } from "@/enums/code";
import { generateModuleId } from "@/utils/uuid";
import { getPageWhere, getPageData, } from '@/utils/page'
import { GetParamPageDto, AddParamDto } from './param.dto';
@Injectable()
export class ParamService {

  constructor(
    @InjectModel(Param)
    private paramModel: typeof Param,
  ) { }

  /**
   * 获取系统参数分页
   * @method get
   * @param getParamPageDto 
   * @returns 
   */
  async getParamPage(getParamPageDto: GetParamPageDto) {
    const { current, size } = getParamPageDto;
    const { count, rows } = await this.paramModel.findAndCountAll({
      where: {},
      ...getPageWhere(current, size)
    });
    return {
      code: RESPONSE_CODE.SUCCESS,
      data: getPageData(current, size, count, rows),
      msg: null,
    }
  }

  /**
   * 添加param
   * @method post
   * @param addParamDto 
   * @returns 
   */
  async addParam(addParamDto: AddParamDto) {
    await this.paramModel.create({
      ...addParamDto,
      publicId: generateModuleId(),
    });
    return {
      code: RESPONSE_CODE.SUCCESS,
      data: null,
      msg: null,
    }
  }

  async getPvByCode(code: string) {
    return {
      code: RESPONSE_CODE.SUCCESS,
      data: code,
      msg: null,
    }
  }

  async getPublicValueByCode(code: string) {
    // const param = await Param.findOne({ where: { publicKey: code } });
    return {
      code: RESPONSE_CODE.SUCCESS,
      data: null,
      msg: null,
    }
  }
}
